package com.miniserver.tql.database.impl;

import java.util.concurrent.ConcurrentHashMap;

import com.miniserver.tql.database.model.TAttr;
import com.miniserver.tql.database.model.TNode;

public class ModelImpl implements TNode {
	private String name;
	/* private String value; */
	private TNode[] elements = null;
	private ConcurrentHashMap<String, TAttr> attrs = new ConcurrentHashMap<String, TAttr>();

	public ModelImpl(String name/* , String value */) {
		this.name = name;
	}

	private static class AttrImpl implements TAttr {
		private String value = null;

		private AttrImpl(String val) {
			this.value = val;
		}

		@Override
		public String getValue() {
			return this.value;
		}
	}

	// FIXME
	/* 猜测只读的简单数组应该是线程安全的 */
	/* 如果仍然不安全, 可以synchronized此方法, clone一个新数组 */
	@Override
	public TNode[] getChildren() {
		return this.elements;
	}

	@Override
	public TAttr getAttribute(String key) {
		if (this.attrs.containsKey(key)) {
			return this.attrs.get(key);
		} else {
			return null;
		}
	}

	@Override
	public String getNodeName() {
		return this.name;
	}

	public void putAttribute(String key, String attr) {
		this.attrs.put(key, new AttrImpl(attr));
	}

	public void setChildren(TNode[] elements) {
		this.elements = elements;
	}
}
